package com.example.boot.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.exception.CustomException;
import com.example.pojo.dto.RoleDTO;
import com.example.pojo.entity.Role;
import com.example.pojo.entity.RouteItem;
import com.example.service.SystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TODO
 *
 * @author ding
 * @since 2024/7/3
 */
@RestController
@RequestMapping("/system")
@Api(tags = "系统管理相关接口")
public class SystemController {

    @Autowired
    private SystemService systemService;


    @ApiOperation("获取菜单信息  管理菜单信息")
//    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @GetMapping("/getMenuList")
    public Result getAllMenu(String menuName, String status){
        List<RouteItem> routeItemList = systemService.getAllMenu(menuName, status);
        return Result.success(routeItemList);
    }

    @ApiOperation("获取角色信息")
//    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @GetMapping("/getRoleListByPage")
    public Result getRoleListByPage(@ApiParam("角色名称") String roleName,
                                    @ApiParam("角色状态") Integer status,
                                    @RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize){
        Page<Role> rolePage = systemService.getRoleListByPage(roleName, status, pageNum, pageSize);
        return Result.success(rolePage);
    }

    @ApiOperation("设置角色状态(禁用或启用)")
//    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @PutMapping("/setRoleStatus")
    public Result setRoleStatus(@RequestBody Role role){
        if (ObjectUtil.isEmpty(role)) {
            throw new CustomException(ResultCodeEnum.PARAM_LOST_ERROR);
        }
        systemService.setRoleStatus(role);
        return Result.success();
    }

    @ApiOperation("上传角色信息，判断是新增还是修改")
//    @PreAuthorize("hasAuthority('ROLE_SUBMIT')")
    @PostMapping("/submitRole")
    public Result submitRole(@RequestBody RoleDTO roleDTO) {
        if (ObjectUtil.isEmpty(roleDTO)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        systemService.submitRole(roleDTO);
        return Result.success();
    }

    @ApiOperation("删除角色信息")
//    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @DeleteMapping("/deleteRole/{id}")
    public Result DeleteRole(@ApiParam("删除角色id") @PathVariable Integer id) {
        systemService.DeleteRole(id);
        return Result.success();
    }


}
